<template>
	<div>
		<el-row style="height: 50px;line-height: 50px;text-align: left;">
			<el-button type="primary" @click="addNew">新增情景模式</el-button>
		</el-row>
		<button id="123" @click="getData1()" style="visibility:hidden">获取值</button>
		<el-table :data="tableData" stripe style="width: 100%">
			<el-table-column label="情景名称">
				<template slot-scope="scope">
					<span v-if="scope.row.scen.trigger == 'action'">设备触发</span>
					<span v-if="scope.row.scen.trigger == 'time'">定时任务</span>
				</template>
			</el-table-column>
			<el-table-column prop="scen[name]" label="名称"></el-table-column>
			<el-table-column prop="scen[remark]" label="触发条件"></el-table-column>
			<el-table-column fixed="right" label="操作" width="350">
				<template slot-scope="scope">
					<el-button @click.native.prevent="updateStops(scope.$index, tableData)">{{scope.row.scen.stops == 0 ? '禁用':'启用'}}</el-button>
					<el-button type="primary" icon="el-icon-edit" circle @click.native.prevent="addDeviceQing(scope.$index, tableData)"></el-button>
					<el-button type="danger" icon="el-icon-delete" circle @click.native.prevent="delectScenarios(scope.$index, tableData)"></el-button>
					<el-button v-if="scope.row.scen.trigger == 'time' " @click.native.prevent="updateTime11(scope.$index, tableData)">时间修改</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-dialog title="修改时间" :visible.sync="dialogFormVisibleUpdate">
				<el-row style="height: 250px;line-height: 50px;text-align: left;">
					<iframe id="dd" src="/src/components/iframHtml/dateTime.html" style="width:100%;height:250px;display:block;float:left" frameborder="0" border="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="yes"></iframe>
				</el-row>
			
			时间选择:   <el-time-picker v-model="updateTime" :picker-options="{selectableRange: '01:00:00 - 23:59:59'}" placeholder="请选择一个时间点" :change="updateData_Time(updateTime)"></el-time-picker>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisibleUpdate = false">取 消</el-button>
				<el-button type="primary" @click="sureUpdate">保 存</el-button>
			</div>
		</el-dialog>
		<el-dialog title="新增情景模式" :visible.sync="dialogFormVisible" :close-on-click-modal="ss" width="80%">
			<el-form :inline="true" :model="form">
				<el-form-item label="情景类型:">
					<el-select v-model="qingjinId" placeholder="请选择情景模式">
						<el-option :label="x.name" :value="x.id" v-for="x in qingJinType"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="填写名称">
					<el-input v-model="name" placeholder="请填写情景模式名称"></el-input>
				</el-form-item>
				<el-form-item v-if="!isShowAddDirect">
                  
					<iframe v-if="qingjinId == 1" id="dd" src="/src/components/iframHtml/dateTime.html" style="width:800px;height:250px;display:block;float:left" frameborder="0" border="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="yes"></iframe>
					
				<span v-if="qingjinId == 1" >时间选择: </span>  <el-time-picker v-if="qingjinId == 1" v-model="updateTime" :picker-options="{selectableRange: '01:00:00 - 23:59:59'}" placeholder="请选择一个时间点" :change="updateData_Time(updateTime)"></el-time-picker>
				</el-form-item>
				<!-- <el-form-item label="是否每天" v-if="qingjinId == 1">
					<el-checkbox v-model="isEveryDay">是</el-checkbox>
        </el-form-item>-->
			</el-form>
			<!-- v-if="isShowAddDirect" -->
			<div v-if="qingjinId != 1">
				<div v-if="isShowAddDirect" style="margin-top:10px">
					<el-from-item>
						<el-cascader :options="treeInBuildData" @change="handleItemChange" :props="props"></el-cascader>
					</el-from-item>
					<el-from-item>
						<el-select v-model="addPreDev.Devid" placeholder="请选择设备">
							<el-option :label="x.name" :value="x.id" v-for="x in deviceData" :key="x.name"></el-option>
						</el-select>
					</el-from-item>
					<!-- 光感强度，温湿度，人感 -->
					<el-from-item>
						<el-select v-model="otherText">
							<el-option :label="x.name" :value="x.name" v-for="x in otherData" :key="x.name"></el-option>
						</el-select>
					</el-from-item>
					<el-input v-model="Express" placeholder="请输入内容" style="width:30%"></el-input>
					<el-button size="mini" @click="sureAddPre()">添加判别设备</el-button>
				</div>

				<el-table :data="qianzhiData" style="width: 100%；margin-top:30px" v-if="isShowAddDirect">
					<el-table-column label="房间名" width="180" prop="buidName"></el-table-column>
					<el-table-column label="设备名" prop="devName" width="180"></el-table-column>
					<el-table-column label="触发条件">
						<template slot-scope="scope">
							<el-input v-model="scope.row.express" placeholder></el-input>
						</template>
					</el-table-column>
					<el-table-column label="操作">
						<template slot-scope="scope">
							<el-button size="mini" type="danger" @click="sureCloseZu(scope.$index, scope.row)">移除</el-button>
							<el-button size="mini" type="danger" @click="sureSaveZu(scope.$index, scope.row)">保存</el-button>
						</template>
					</el-table-column>
				</el-table>
			</div>
			<div v-if="isShowAddDirect" style="margin-top:10px">
				<el-from-item>
					<el-cascader :options="treeInBuildData" @change="handleItemChangeOther" :props="props"></el-cascader>
				</el-from-item>
				<el-select v-model="device_other" filterable placeholder="请选择">
					<el-option v-for="item in deviceDataOne" :key="item.name" :label="item.name" :value="item.id"></el-option>
				</el-select>

				<el-select v-model="addDeviceInScenarios.ActionName" filterable placeholder="请选择按钮" style="margin-top:30px" v-if="isShowType">
					<el-option v-for="item in showTypeData" :key="item.name" :label="item.text" :value="item.name"></el-option>
				</el-select>
				<el-select v-model="addDeviceInScenarios.Actions" placeholder="请选择指令">
					<el-option :key="x.name" :label="x.name" :value="x.actionName" v-for="x in actions"></el-option>
				</el-select>
				
				<el-button size="mini" @click="sureAddDeviceInScenarios()">添加指令</el-button>
				<el-table :data="deviceData_Scenarios" style="width: 100%">
					<el-table-column label="房间名" prop="buildName" width="180"></el-table-column>
					<el-table-column prop="name" label="设备" width="180"></el-table-column>
					<el-table-column label="指令1">
					    <template slot-scope="scope" v-if="scope.row.btnType == 7 || scope.row.btnType == 6 || scope.row.btnType == 8">
								<el-select v-model="scope.row.actionName" placeholder="请选择按钮">
								<el-option v-for="item in scope.row.buttonData" :key="item.id" :label="item.text" :value="item.id" ></el-option>
							</el-select>
						</template>
					</el-table-column>
					<el-table-column width="180">
						<template slot-scope="scope">
                         <el-select v-model="scope.row.actionName" v-if="scope.row.btnType != 6 && scope.row.btnType != 7 && scope.row.btnType != 8" placeholder="请选择情景模式">
							<el-option :key="x.name" :label="x.name" :value="x.actionName" v-for="x in scope.row.updateScenar"></el-option>
						</el-select>
						<el-select v-if="scope.row.btnType == 6 || scope.row.btnType == 7 || scope.row.btnType == 8" v-model="scope.row.actions" placeholder="请选择情景模式">
							<el-option :key="x.name" :label="x.name" :value="x.name" v-for="x in actions"></el-option>
						</el-select>
						</template>
					</el-table-column>
					<el-table-column label="操作">
					  <template slot="header" slot-scope="scope" change-on-select>
       <el-cascader :options="treeInBuildData" @change="searchByRoom" :props="props"></el-cascader>
      </template>
						<template slot-scope="scope">
							<el-button size="mini" type="danger" @click="sureClose(scope.$index, scope.row)">移除</el-button>
							<el-button size="mini" type="danger" @click="saveClose(scope.$index, scope.row)">保存</el-button>
						</template>
					</el-table-column>
				</el-table>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible = false">取 消</el-button>
				<el-button type="primary" @click="sure">保 存</el-button>
			</div>
		</el-dialog>

		<el-dialog title="情景模式下新增设备" :visible.sync="dialogFormVisible_addDevice" :close-on-click-modal="ss" width="80%">
			<el-form :inline="true" :model="form">
				<el-form-item label="填写名称">
					<el-input v-model="addData.name" placeholder="请填写情景模式名称"></el-input>
				</el-form-item>
				<el-form-item label="情景类型:">
					<el-select v-model="addData.qingjinId" placeholder="请选择情景模式" disabled="disable">
						<el-option :key="x.name" :label="x.name" :value="x.id" v-for="x in qingJinType"></el-option>
				</el-select>
				</el-form-item>
				  <el-button type="primary" @click="updateName()">修改名字</el-button>
				<div v-if = "addData.qingjinId != 1">
					<el-from-item>
						<el-cascader :options="treeInBuildData" @change="handleItemChange" :props="props"></el-cascader>
					</el-from-item>
					<el-from-item>
						<el-select v-model="addPreDev.Devid" placeholder="请选择设备">
							<el-option :label="x.name" :value="x.id" v-for="x in deviceData" :key="x.name"></el-option>
						</el-select>
					</el-from-item>
					<!-- 光感强度，温湿度，人感 -->
					<el-from-item>
						<el-select v-model="otherText">
							<el-option :label="x.name" :value="x.name" v-for="x in otherData" :key="x.name"></el-option>
						</el-select>
					</el-from-item>
					<el-input v-model="Express" placeholder="请输入内容" style="width:30%"></el-input>
					<el-button size="mini" @click="sureAddPre()">添加组合设备</el-button>
				</div>
				<el-table :data="qianzhiData" style="width: 100%" v-if = "addData.qingjinId != 1">
					<el-table-column label="房间名" prop="buidName" width="180"></el-table-column>
					<el-table-column label="设备名" prop="devName" width="180"></el-table-column>
					<el-table-column label="触发条件" prop="express">
						<template slot-scope="scope">
							<el-input v-model="scope.row.express" placeholder></el-input>
						</template>
					</el-table-column>
					<el-table-column label="操作">
							<el-button size="mini" type="danger" @click="sureClose(scope.$index, scope.row)">移除</el-button>
						<template slot-scope="scope">
							<el-button size="mini" type="danger" @click="sureCloseZu(scope.$index, scope.row)">移除</el-button>
							<el-button size="mini" type="danger" @click="sureSaveZu(scope.$index, scope.row)">保存</el-button>
						</template>
					</el-table-column>
				</el-table>
			</el-form>
			<el-from-item>
				<el-cascader :options="treeInBuildData" @change="handleItemChangeOther" :props="props"></el-cascader>
			</el-from-item>
			<el-select v-model="device_other" filterable placeholder="请选择" style="margin-top:30px">
				<el-option v-for="item in deviceDataOne" :key="item.name" :label="item.name" :value="item.id"></el-option>
			</el-select>

			<el-select v-model="addDeviceInScenarios.ActionName" filterable placeholder="请选择按钮" style="margin-top:30px" v-if="isShowType">
				<el-option v-for="item in showTypeData" :key="item.name" :label="item.text" :value="item.name"></el-option>
			</el-select>

			<el-select v-model="addDeviceInScenarios.Actions" placeholder="请选择指令">
				<el-option :key="x.name" :label="x.name" :value="x.actionName" v-for="x in actions"></el-option>
			</el-select>
			<el-button size="mini" @click="sureAddDeviceInScenarios()">添加</el-button>
			<el-table :data="deviceData_Scenarios" style="width: 100%">
				<el-table-column label="房间名" prop="buildName" width="180">
				</el-table-column>
				<el-table-column prop="name" label="设备" width="180"></el-table-column>
				<el-table-column label="指令1">
					  <template slot-scope="scope" v-if="scope.row.btnType == 7 || scope.row.btnType == 6 || scope.row.btnType == 8">
								<el-select v-model="scope.row.actionName" placeholder="请选择按钮">
								<el-option v-for="item in scope.row.buttonData" :key="item.id" :label="item.text" :value="item.id" ></el-option>
							</el-select>
						</template>
				</el-table-column>
				<el-table-column label="指令" width="180">
					<template slot-scope="scope">
						<el-select v-model="scope.row.actionName" v-if="scope.row.btnType != 6 && scope.row.btnType != 7 && scope.row.btnType != 8" placeholder="请选择情景模式">
							<el-option :key="x.name" :label="x.name" :value="x.actionName" v-for="x in scope.row.updateScenar"></el-option>
						</el-select>
						<el-select v-if="scope.row.btnType == 6 || scope.row.btnType == 7 || scope.row.btnType == 8" v-model="scope.row.actions" placeholder="请选择情景模式">
							<el-option :key="x.name" :label="x.name" :value="x.name" v-for="x in actions"></el-option>
						</el-select>
					</template>
				</el-table-column>

				<el-table-column  align="right">
				  <template slot="header" slot-scope="scope">
       <el-cascader :options="treeInBuildData" @change="searchByRoom" change-on-select :props="props"></el-cascader>
      </template>
					<template slot-scope="scope">
						<el-button size="mini" type="danger" @click="sureClose(scope.$index, scope.row)">移除</el-button>
						<el-button size="mini" type="danger" @click="saveClose(scope.$index, scope.row)">保存</el-button>
					</template>
				</el-table-column>




			</el-table>

			<div slot="footer" class="dialog-footer">
			
				<el-button @click="dialogFormVisible_addDevice = false">取 消</el-button>
				<el-button type="primary" @click="dialogFormVisible_addDevice = false">确 定</el-button>
			</div>
		</el-dialog>
	</div>
	<!--<div class="block">
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage_all" :page-sizes="[15, 30, 45, 50]" :page-size="15" layout="total, sizes, prev, pager, next, jumper" :total="allCount">
			</el-pagination>
  </div>-->
</template>

<script>
	function callback() {
		console.warn("asdf");
	}
	export default {
		data() {
			return {
				actionNameType: "", //判断它是有按钮设备还是普通没有的
				buttonSevenData: [{
						id: "1",
						value: "0",
						status: "1",
						text: ""
					},
					{
						id: "2",
						value: "0",
						status: "1",
						text: ""
					},
					{
						id: "3",
						value: "0",
						status: "1",
						text: ""
					},
					{
						id: "4",
						value: "0",
						status: "1",
						text: ""
					},
					{
						id: "5",
						value: "0",
						status: "1",
						text: ""
					},
					{
						id: "6",
						value: "0",
						status: "1",
						text: ""
					},
					{
						id: "7",
						value: "0",
						status: "1",
						text: ""
					},
					{
						id: "8",
						value: "0",
						status: "1",
						text: ""
					}
				],
				showTypeData: [],
				buttonSixData: {
					btns: [{
							id: "1",
							status: 0,
							text: "",
							value: ""
						},
						{
							id: "2",
							status: 0,
							text: "",
							value: ""
						},
						{
							id: "3",
							status: 0,
							text: "",
							value: ""
						},
						{
							id: "4",
							status: 0,
							text: "",
							value: ""
						}
					],
					value: ""
				},
				buttonEvenData: {
					btns: [{
							id: "1",
							value: "0",
							status: "1",
							text: ""
						},
						{
							id: "2",
							value: "0",
							status: "1",
							text: ""
						}
					],
					values: ["0", "0", "0", "0", "0", "0", "0", "0"]
				},
				isShowDate: false,
				isShowType: false,
				ss: false,
				updateTime: new Date(),
				value2: new Date(),
				value4: "",
				otherData: [],
				otherText: "",
				props: {
					value: "id",
					label: "name",
					children: "sub"
				},
				updateTimes: "",
				isEveryDay: true, //是否每天
				addPreDev: {
					//添加前置设备的值
					Devid: "",
					Express: "",
					ScenariosId: ""
				},
				actions: [{
						name: "打开",
						actionName: "打开"
					},
					{
						name: "关闭",
						actionName: "关闭"
					}
				],
				isShowAddDirect: false,
				device_other: "",
				allCount: "", //总台数
				currentPage_all: "", //总页数
				actions_device: [],
				deviceData_Scenarios: [],
				tableData: [],
				addDeviceInScenarios: {
					ScenariosID: "",
					DeviceID: "",
					ActionName: ""
				},
				addData: {
					name: "",
					qingjinId: "",
					deviceName: "",
					action: "",
					number: ""
				},
				dialogFormVisible_addDevice: false,
				addActionId: "",
				selectDeviceData: "", //选择设备的id
				number: "",
				action: "",
				isAdd: "true",
				dialogFormVisible: false,
				louYuData: [],
				louYuId: "",
				louChenData: [],
				louChenId: "",
				roomData: [],
				roomId: "",
				deviceData: [],
				deviceId: "",
				scenariosId: "",
				addScenariosData: {
					Name: "", //情景模式名字
					Trigger: "action", //触发方式
					ExecNow: true, //是否立即执行
					ExecTime: "", //定时执行
					ExecDateStart: "", //开始时间
					ExecDateEnd: "", //结束时间
					Role: "", //规则名称
					PreDeviceID: "", //前置设备
					PreAction: "", //前置动作
					PreValue: "", //前置动作值
					Expression: "" //匹配表达式
				},

				allDeviceData: [],
				getTypeData: {
					Pager: true,
					PageIndex: 1,
					PageSize: 10
				},
				contextualModelList: [], //整个情景模式列表
				contextualModelItem: {
					deivceId: "",
					deviceName: "",
					condition: ""
				},
				qianzhiData: [],
				getDeviceData: {
					BuildID: "",
					Pager: false
				},
				getBuildData: {
					ParentId: "",
					Pager: false
				},
				//获取情景模式下的前置设备
				GetPreDevsData: {
					ScenId: ""
				},
				Express: "", //触发条件
				qingjinId: 1,
				GetActionsData: {
					//根据设备去获取指令
					DeviceID: ""
				},
				allActionsData: [],
				deviceDataOne: [],
				dialogFormVisibleUpdate: false,
				updateDateId: "",
				qingJinType: [{
						name: "定时任务",
						id: 1
					},
					{
						name: "设备触发",
						id: 2
					}
				]
			};
		},
		methods: {
			//
			searchByRoom(val){
				var buildId = val[val.length - 1];
				this.choseBuild = buildId;
				this.getDeviceInScenarios();
			},
			//时间控件的显示
			updateTime11(index, data) {
				this.dialogFormVisibleUpdate = true;
				debugger
				this.updateDateId = data[index].scen.id;
				if(data[index].scen.execTime == "" || data[index].scen.execTime == undefined){
	               this.updateTime = new Date();
				}else{
                   this.updateTime = new Date(data[index].scen.execTime);
				}
			
			},
			//启用禁用按钮
			updateStops(index, data) {
				var boold;
				if(data[index].scen.stops == 0) {
					//说明要把它禁掉
					boold = true;
				} else {
					//
					boold = false;
				}
				var data = {
					ID: data[index].scen.id,
					Running: boold
				};
				this.$httpRequest.post("api/Scenarios/SetStop", data, res => {
					if(res.isSuccess) {
						this.$notify({
							title: "修改",
							message: "成功",
							type: "success"
						});
					}
					this.getData();
				});
			},
			//保存时间
			sureUpdate() {
				var data = {
					ID: this.updateDateId,
					Date: document.getElementById("123").value,
					Times: this.updateTimes
				};
				this.$httpRequest.post("api/Scenarios/UpdateDate", data, res => {
					if(res.isSuccess) {
						this.$notify({
							title: "修改时间",
							message: "成功",
							type: "success"
						});
					}
					this.getData();
				});
			},
			//情景模式更改名字
			updateName(){
			   var data = {
				   ID:this.scenariosId,
				   Name:this.addData.name
			   }
			   this.$httpRequest.post('api/Scenarios/UpdateName',data,res=>{
                 if(res.isSuccess){
					 	this.$notify({
							title: "更新情景模式名字",
							message: "成功",
							type: "success"
						});
						this.getData();
				 }else{

				 }
			   })
			},
			//日期控件改变的方法
			dateChange(e) {
				if(e == "") {} else {
					var startMonth = e[0].getMonth() + 1;
					var endMonth = e[1].getMonth() + 1;
					this.addScenariosData.ExecDateStart =
						e[0].getFullYear() + "-" + startMonth + "-" + e[0].getDay();
					this.addScenariosData.ExecDateEnd =
						e[1].getFullYear() + "-" + endMonth + "-" + e[1].getDay();
				}
			},
			//时间控件发生改变
			timeChange(e) {
				if(e == "") {} else {
					this.addScenariosData.ExecTime = e.getHours() + ":" + e.getDate();
				}
			},
			//更改时间控件里面的时间更改
			updateData_Time(e) {
				if(e == "") {} else {
					this.updateTimes = e.getHours() + ":" + e.getMinutes();
				}
			},
			//获取ifrom的值
			getData1() {
				alert(document.getElementById("123").value);
			},
			//确定添加前置设备的方法
			sureAddPre() {
				this.addPreDev.Express = this.otherText + this.Express;
				this.addPreDevHttp();
			},

			//删除前置设备
			sureCloseZu(index, item) {
				var data = {
					ID: item.id
				};
				this.$httpRequest.post("api/Scenarios/DelPreDev", data, res => {
					if(res.isSuccess) {
						this.$notify({
							title: "删除组合设备成功",
							message: "成功",
							type: "success"
						});
					}
					this.GetPreDevs();
					this.getData();
				});
			},
			//保存前置设备
			saveClose(index, item) {
				var data = {
					ID: item.id,
					Actions: item.actions,
					ActionName: item.actionName
				};
				this.$httpRequest.post("api/Scenarios/UpdateDevices", data, res => {
					if(res.isSuccess) {
						this.$notify({
							title: "保存",
							message: "成功",
							type: "success"
						});
					}
				});
			},
			sureSaveZu(index, item) {
				this.updateQianZhiDevice(item.id, item.express);
			},
			//更改设备的方法
			updateQianZhiDevice(id, text) {
				var data = {
					ID: id,
					Express: text
				};
				this.$httpRequest.post("api/Scenarios/UpdatePreDev", data, res => {
					if(res.isSuccess) {
						this.$notify({
							title: "更改前置设备",
							message: "成功",
							type: "success"
						});
						this.getData();
					}
				});
			},
			//添加前置设备的请求
			addPreDevHttp() {
				this.addPreDev.ScenariosId = this.scenariosId;
				this.GetPreDevsData.ScenId = this.scenariosId;
				this.$httpRequest.post(
					"api/Scenarios/AddPreDev",
					this.addPreDev,
					response => {
						if(response.isSuccess) {
							this.$notify({
								title: "添加组合设备成功",
								message: "成功",
								type: "success"
							});
							this.GetPreDevs();
							this.getData();
						} else {}
					}
				);
			},
			//获取前置设备的请求
			GetPreDevs() {
				this.GetPreDevsData.ScenId = this.scenariosId;
				this.$httpRequest.post(
					"api/Scenarios/GetPreDevs",
					this.GetPreDevsData,
					response => {
						if(response.isSuccess) {
							this.qianzhiData = [];
							this.qianzhiData = response.data;
						} else {}
					}
				);
			},
			//			handleSizeChange(val) {
			//				this.getTypeData.PageSize = val;
			//				this.getTypeData.PageIndex = 0;
			//				this.getData();
			//			},
			//			handleCurrentChange(val) {
			//				this.getTypeData.PageIndex = val - 1;
			//				this.getData();
			//			},

			//楼层选择
			handleItemChange(val) {
				var buildId = val[val.length - 1];
				(this.deviceData = []),
				(this.deviceId = ""),
				(this.getDeviceData.BuildID = buildId);
				this.getDeviceData.isReadDev = 1;
				this.getDeviceByConfig(this.getDeviceData);
			},
			handleItemChangeOther(val) {
				var buildId = val[val.length - 1];
				(this.deviceDataOne = []),
				(this.deviceId = ""),
				(this.getDeviceData.BuildID = buildId);
				this.getDeviceByConfig1(this.getDeviceData);
			},
			//情景模式下面添加设备
			addDeviceQing(index, row) {
				this.otherData = [];
				this.otherText = "";
				this.showTypeData = [];
				this.isShowType = false;
				this.Express = "";
				this.getBuildDataInTree();
				this.deviceData = [];

				this.deviceDataOne = [];
				this.device_other = "";
				(this.addDeviceInScenarios = {
					ScenariosID: "",
					DeviceID: "",
					ActionName: ""
				}),
				(this.addPreDev.Devid = "");
				this.addPreDev.Express = "";
				this.dialogFormVisible_addDevice = true;
				this.addData.name = row[index].scen.name;
				debugger
				switch(row[index].scen.trigger) {
					case "time":
						this.addData.qingjinId = 1;
						break;
					case "action":
						this.addData.qingjinId = 2;
						break;
					case "batch":
						this.addData.qingjinId = 3;
						break;
				}
				//this.addData.qingjinId = row[index].scen.trigger == "time" ? 1 : 2;
				this.addData.deviceName = row[index].name;
				this.addData.action = row[index].scen.preAction;
				this.addData.num = row[index].scen.role;
				this.scenariosId = row[index].scen.id;
				//this.getDeviceByConfig("");
				this.GetPreDevs();
				var data = {
					DeviceID: row[index].scen.preDeviceID
				};
				this.getDeviceInScenarios();
				this.$httpRequest.get("api/Device/GetActions", data, response => {
					if(response.isSuccess) {
						this.actions_device = response.data;
					} else {}
				});
			},
			//获取楼层数据书结构
			getBuildDataInTree() {
				this.$httpRequest.get("api/Build/GetBuildTree", "", res => {

					this.treeInBuildData = res.data;
				});
			},
			//情景模式为批量处理的操作
			pLDo() {
				var data = {
					ID: this.scenariosId
				};
				this.$httpRequest.post("api/Scenarios/Exec", data, response => {
					if(response.isSuccess) {
						this.$notify({
							title: "批量执行",
							message: "成功",
							type: "success"
						});
					} else {
						this.$notify({
							title: "批量执行失败",
							message: response.msg,
							type: "error"
						});
					}
					this.getDeviceInScenarios();
				});
			},
			//情景模式下设备的删除
			sureClose(index, row) {
				var data = {
					ID: row.id
				};
				this.$httpRequest.post("api/Scenarios/DelDevices", data, response => {
					if(response.isSuccess) {
						this.$notify({
							title: "删除设备",
							message: "成功",
							type: "success"
						});
					} else {
						this.$notify({
							title: "删除设备失败",
							message: response.msg,
							type: "error"
						});
					}
					this.getDeviceInScenarios();
				});
			},
			//删除情景模式
			delectScenarios(index, row) {
				var data = {
					ScenID: row[index].scen.id
				};
				this.$httpRequest.post("api/Scenarios/Delete", data, res => {
					if(res.isSuccess) {
						this.$notify({
							title: "删除情景模式",
							message: "成功",
							type: "success"
						});
					} else {
						this.$notify({
							title: "删除情景模式失败",
							message: res.msg,
							type: "error"
						});
					}
					this.getData();
				});
			},
			//添加设备在情景模式下
			sureAddDeviceInScenarios() {
				for(let i = 0; i < this.actions.length; i++) {
					if(this.actions[i].actionName == this.addDeviceInScenarios.Actions) {
						this.addDeviceInScenarios.Actions = this.actions[i].name;
						if(this.actionNameType) {
							//为true则不操作actionName,为false则操作actionName为英文
						} else {
							this.addDeviceInScenarios.ActionName = this.actions[i].actionName;
						}
						break;
					}
				}
				this.addDeviceInScenarios.ScenariosID = this.scenariosId;
				this.$httpRequest.post(
					"api/Scenarios/AddDevice",
					this.addDeviceInScenarios,
					response => {
						if(response.isSuccess) {
							this.$notify({
								title: "情景模式下添加设备",
								message: "成功",
								type: "success"
							});
							this.deviceData_Scenarios = [];
							this.getDeviceInScenarios();
						} else {
							this.$notify({
								title: "新增失败",
								message: response.msg,
								type: "error"
							});
						}
					}
				);
			},
			//获取情景模式下的设备
			getDeviceInScenarios() {
				this.deviceData_Scenarios = [];
				// this.$httpRequest.get('api/Scenarios/GetDevices', data, response => {
				// 	if(response.isSuccess) {
				// 	  this.deviceData_Scenarios = response.data;
				// 	}
				// })
				var data = {
					ScenID: this.scenariosId,
					BuidId:this.choseBuild
				};
				this.$httpRequest.get("api/Scenarios/GetDevices", data, response => {
					if(response.isSuccess) {
						this.deviceData_Scenarios = response.data;
						for(let i = 0; i < this.deviceData_Scenarios.length; i++) {
							let buttonData = [];
                           var data = {
							DevID: this.deviceData_Scenarios[i].deviceID
						    };
                            if(this.deviceData_Scenarios[i].btnType == 6){
                               this.$httpRequest.get("api/Device/GetWater", data, response => {
								if(response.data.length == 0) {
									var data1 = {
										btns: [{
												id: "1",
												status: "1",
												text: "",
												name: 1
											},
											{
												id: "2",
												status: "1",
												text: "",
												name: 2
											},
											{
												id: "3",
												status: "1",
												text: "",
												name: 3
											},
											{
												id: "4",
												status: "1",
												text: "",
												name: 4
											}
										],
										value: 30
									};
								
									buttonData = data1.btns;
								} else {
									for(let i = 0; i < response.data.btns.length; i++) {
										response.data.btns[i].name = i + 1;
										if(response.data.btns[i].text == "") {
											response.data.btns[i].text == i + 1;
										}
									}
									buttonData = response.data.btns;
								}
								this.$set(
									this.deviceData_Scenarios[i],
									"buttonData",
									buttonData
								)
							});
							}else if(this.deviceData_Scenarios[i].btnType == 7){
	this.$httpRequest.get("api/Device/GetAir1", data, response => {
								if(response.data.length == 0) {
									var data1 = [{
											id: "1",
											value: "0",
											status: "1",
											text: "",
											name: 1
										},
										{
											id: "2",
											value: "0",
											status: "1",
											text: "",
											name: 2
										},
										{
											id: "3",
											value: "0",
											status: "1",
											text: "",
											name: 3
										},
										{
											id: "4",
											value: "0",
											status: "1",
											text: "",
											name: 4
										},
										{
											id: "5",
											value: "0",
											status: "1",
											text: "",
											name: 5
										},
										{
											id: "6",
											value: "0",
											status: "1",
											text: "",
											name: 6
										},
										{
											id: "7",
											value: "0",
											status: "1",
											text: "",
											name: 7
										},
										{
											id: "8",
											value: "0",
											status: "1",
											text: "",
											name: 8
										}
									];
									buttonData = data1
								} else {
									for(let i = 0; i < response.data.length; i++) {
										response.data[i].name = i + 1;
										if(response.data[i].text == "") {
											response.data[i].text = i + 1;
										}
									}
									buttonData = response.data
								}
								this.$set(
									this.deviceData_Scenarios[i],
									"buttonData",
									buttonData
								)
							});
							}else if(this.deviceData_Scenarios[i].btnType == 8){
	                  this.$httpRequest.get("api/Device/GetAir2", data, response => {
								if(response.data.length == 0) {
									var data = {
										btns: [{
												id: "1",
												value: "0",
												status: "1",
												text: "",
												name: 1
											},
											{
												id: "2",
												value: "0",
												status: "1",
												text: "",
												name: 2
											}
										],
										values: ["0", "0", "0", "0", "0", "0", "0", "0"]
									};
									buttonData = data.btns
								} else {
									for(let i = 0; i < response.data.btns.length; i++) {
										response.data.btns[i].name = i + 1;
										if(response.data.btns[i].text == "") {
											response.data.btns[i].text == i + 1;
										}
									}
									buttonData = response.btns
								}
								this.$set(
									this.deviceData_Scenarios[i],
									"buttonData",
									buttonData
								)
							})
							}else{
								buttonData = [];
							}
							var data = {
								DeviceID: this.deviceData_Scenarios[i].deviceID
							};
							
							this.$httpRequest.get("api/Device/GetActions", data, response => {
								this.$set(
									this.deviceData_Scenarios[i],
									"updateScenar",
									response.data
								);
								
							});
						}
						// for(let i =0;i<sss.length;i++){
						// 	sss[i].updateScenar = [];
						// }
						// 	this.deviceData_Scenarios = sss;
					} else {}
				});
			},

			//关于添加设备时的设备的选择
			sureAdd(index, row) {},

			//确定添加设备
			sure() {
				this.addScenariosData.Name = this.name;
				this.addScenariosData.ExecNow = this.isEveryDay;
				// if(this.isEveryDay){
				//           this.addScenariosData.ExecDateStart = "";
				// this.addScenariosData.ExecDateEnd = "";
				// }else{
				//   this.addScenariosData.ExecTime = "";
				// }
				switch(this.qingjinId) {
					case 1:
						this.addScenariosData.Trigger = "time";
						break;
					case 2:
						this.addScenariosData.Trigger = "action";
						break;
					default:
						this.addScenariosData.Trigger = "batch";
						break;
						//this.addScenariosData.Trigger = this.qingjinId == 1 ? "time" : "action": "batch";
				}
				this.addScenariosData.Experssion = this.number;
				this.addScenariosData.PreDeviceID = this.deviceId;
				this.addScenariosData.PreAction = this.action;
				this.addScenariosData.ExecDates = document.getElementById("123").value;
				this.addScenariosData.ExecTime = this.updateTimes;
				if(this.scenariosId == "") {
					//说明他是添加情景模式的
					//保存情景模式
					this.$httpRequest.post(
						"api/Scenarios/Add",
						this.addScenariosData,
						response => {
							if(response.isSuccess) {
								this.$notify({
									title: "新增情景模式",
									message: "成功",
									type: "success"
								});
								this.isShowDate = false;
								this.isShowAddDirect = true;
								this.deviceData_Scenarios = [];
								this.scenariosId = response.data;
								this.GetPreDevs();
								//this.dialogFormVisible = false
							} else {
								this.$notify({
									title: "新增情景模式失败",
									message: response.msg,
									type: "erro"
								});
							}
							this.getData();
						}
					);
				} else {
					this.dialogFormVisible = false;
				}
			},
			//一进来获取数据的方法
			getData() {
				var that = this;
				this.$httpRequest.get(
					"api/Scenarios/GetList",
					this.getTypeData,
					response => {
						if(response.isSuccess) {
							var data = response.data;
							that.tableData = data;
							//						this.allCount = response.dataCount, //总台数
							//					  this.currentPage_all = this.allCount / this.getTypeData.PageSize;
						} else {}
					}
				);
			},
			//获取楼层的方法
			getLouData(data, res) {
				this.$httpRequest.get("api/Build/GetList", data, response => {
					if(response.isSuccess) {
						res(response);
					} else {}
				});
			},
			//获取某个条件下的设备
			getDeviceByConfig(data) {
				this.deviceData = [];
				this.$httpRequest.get("api/Device/GetList", data, response => {
					if(response.isSuccess) {
						this.deviceData = response.data;
						this.getDeviceData.isReadDev = -1;
					} else {}
				});
			},
			//获取某个条件下的设备
			getDeviceByConfig1(data) {
				this.deviceDataOne = [];
				var that = this;
				this.$httpRequest.get("api/Device/GetList", data, response => {
					if(response.isSuccess) {
						that.deviceDataOne = response.data;
					} else {}
				});
			},
			//获取设备下的指令
			GetActions() {
				this.$httpRequest.get(
					"api/Device/GetActions",
					this.GetActionsData,
					response => {
						if(response.isSuccess) {
							this.actions = response.data;
						} else {}
					}
				);
			},
			//弹窗的弹出方法
			addNew() {
				this.otherData = [];
				this.otherText = "";
				this.Express = "";
				this.getBuildDataInTree();
				this.qingjinId = "";
				this.name = "";
				this.isShowAddDirect = false;
				this.addPreDev.Devid = "";
				this.addPreDev.Express = "";
				this.deviceData = [];
				this.deviceDataOne = [];
				(this.scenariosId = ""),
				(this.louYuData = []),
				(this.louYuId = ""),
				(this.louChenData = []),
				(this.louChenId = ""),
				(this.roomData = []),
				(this.roomId = ""),
				(this.deviceData = []),
				(this.deviceId = ""),
				(this.dialogFormVisible = true);
				//刚进来的时候获取的是楼宇的数据
				this.getBuildData.ParentId = "";
				this.device_other = "";
				(this.addDeviceInScenarios = {
					ScenariosID: "",
					DeviceID: "",
					ActionName: ""
				}),
				this.getLouData(this.getBuildData, res => {
					this.louYuData = res.data;
				});
			}
		},
		//弹框下面的监听事件
		watch: {
			addPreDev: {
				//深度监听，可监听到对象、数组的变化
				handler(val, oldVal) {
					this.otherText = "";
					for(let i = 0; i < this.deviceData.length; i++) {
						if(this.deviceData[i].id == val.Devid) {
							if(this.deviceData[i].name == "温湿度") {
								this.otherData = [{
										name: "温度"
									},
									{
										name: "湿度"
									}
								];
								this.otherText = "温度";
							} else {
								this.otherData = [{
									name: "x"
								}];
								this.otherText = "x";
							}
						}
					}
				},
				deep: true
			},
			louYuId: function(newId, oldId) {
				//楼宇id的变化
				(this.louChenData = []),
				(this.louChenId = ""),
				(this.getBuildData.ParentId = newId);
				this.getLouData(this.getBuildData, res => {
					this.louChenData = res.data;
				});
			},
			qingjinId: function(newId, oldId) {
				//情景id的变化
				if(newId == 1) {
					this.isShowDate = true;
				} else {
					this.isShowDate = false;
				}
			},
			louChenId: function(newId, oldId) {
				(this.roomData = []),
				(this.roomId = ""),
				(this.getBuildData.ParentId = newId);
				this.getLouData(this.getBuildData, res => {
					this.roomData = res.data;
				});
			},
			roomId: function(newId, oldId) {
				(this.deviceData = []),
				(this.deviceId = ""),
				(this.getDeviceData.BuildID = newId);
				this.getDeviceByConfig(this.getDeviceData);
			},
			deviceId: function(newId, oldId) {
				this.zhiLinData = [];
				this.GetActionsData.DeviceID = newId;
				this.GetActions();
			},

			//情景模式下面设备新增设备的变化
			device_other: function(newId, oldId) {
				this.addDeviceInScenarios.DeviceID = newId;
				this.GetActionsData.DeviceID = newId;
				for(let i = 0; i < this.deviceDataOne.length; i++) {
					if(this.deviceDataOne[i].id == newId) {
						var data = {
							DevID: newId
						};
						if(this.deviceDataOne[i].btnType == 6) {
								this.actions= [{
						name: "打开",
						actionName: "打开"
					},
					{
						name: "关闭",
						actionName: "关闭"
					}
				],
							//水流量4个
							this.isShowType = true;
							this.actionNameType = true;
							this.showTypeData = [];
							this.$httpRequest.get("api/Device/GetWater", data, response => {
								if(response.data.btns.length == 0) {
									var data1 = {
										btns: [{
												id: "1",
												status: "1",
												text: "",
												name: 1
											},
											{
												id: "2",
												status: "1",
												text: "",
												name: 2
											},
											{
												id: "3",
												status: "1",
												text: "",
												name: 3
											},
											{
												id: "4",
												status: "1",
												text: "",
												name: 4
											}
										],
										value: 30
									};
									for(let i = 0; i < data1.btns.length; i++) {
										this.$set(this.showTypeData, i, data1[i].btns);
									}
									//this.showTypeData = data1.btns;
								} else {
									for(let i = 0; i < response.data.btns.length; i++) {
										response.data.btns[i].name = i + 1;
										if(response.data.btns[i].text == "") {
											response.data.btns[i].text == i + 1;
										}
										this.$set(this.showTypeData, i, response.data.btns[i]);
									}
								}
								console.log(this.showTypeData);
							});
						} else if(this.deviceDataOne[i].btnType == 7) {
							//8个
									this.actions= [{
						name: "打开",
						actionName: "打开"
					},
					{
						name: "关闭",
						actionName: "关闭"
					}
				],
							this.isShowType = true;
							this.actionNameType = true;
							this.showTypeData = [];
							this.$httpRequest.get("api/Device/GetAir1", data, response => {
								if(response.data.length == 0) {
									var data1 = [{
											id: "1",
											value: "0",
											status: "1",
											text: "",
											name: 1
										},
										{
											id: "2",
											value: "0",
											status: "1",
											text: "",
											name: 2
										},
										{
											id: "3",
											value: "0",
											status: "1",
											text: "",
											name: 3
										},
										{
											id: "4",
											value: "0",
											status: "1",
											text: "",
											name: 4
										},
										{
											id: "5",
											value: "0",
											status: "1",
											text: "",
											name: 5
										},
										{
											id: "6",
											value: "0",
											status: "1",
											text: "",
											name: 6
										},
										{
											id: "7",
											value: "0",
											status: "1",
											text: "",
											name: 7
										},
										{
											id: "8",
											value: "0",
											status: "1",
											text: "",
											name: 8
										}
									];
									for(let i = 0; i < data1.length; i++) {
										this.$set(this.showTypeData, i, data1[i]);
									}
								} else {
									for(let i = 0; i < response.data.length; i++) {
										response.data[i].name = i + 1;
										if(response.data[i].text == "") {
											response.data[i].text = i + 1;
										}
										this.$set(this.showTypeData, i, response.data[i]);
									}
								}
								console.log(this.showTypeData);
							});
						} else if(this.deviceDataOne[i].btnType == 8) {
							//2个
									this.actions= [{
						name: "打开",
						actionName: "打开"
					},
					{
						name: "关闭",
						actionName: "关闭"
					}
				],
							this.isShowType = true;
							this.actionNameType = true;
							this.showTypeData = [];
							this.$httpRequest.get("api/Device/GetAir2", data, response => {
								if(response.data.length == 0) {
									var data = {
										btns: [{
												id: "1",
												value: "0",
												status: "1",
												text: "",
												name: 1
											},
											{
												id: "2",
												value: "0",
												status: "1",
												text: "",
												name: 2
											}
										],
										values: ["0", "0", "0", "0", "0", "0", "0", "0"]
									};
									for(let i = 0; i < data.btns.length; i++) {
										this.$set(this.showTypeData, i, data.btns[i]);
									}
								} else {
									for(let i = 0; i < response.data.btns.length; i++) {
										response.data.btns[i].name = i + 1;
										if(response.data.btns[i].text == "") {
											response.data.btns[i].text == i + 1;
										}
										this.$set(this.showTypeData, i, response.data.btns[i]);
									}
									this.showTypeData = response.btns;
								}
								console.log(this.showTypeData);
							});
						} else {
							this.actionNameType = false;
							this.isShowType = false;
							this.showTypeData = [];
							this.GetActions();
						}
						
					}
				}
			},
			//监听当前设备发生变化
			selectDeviceData: function(newId, old) {
				this.contextualModelItem = {
					deivceId: "",
					deviceName: "",
					conditionType: ""
				};
				this.contextualModelItem.deivceId = newId; {
					//一条情景模式的条目
					for(let i = 0; i < this.allDeviceData.length; i++) {
						if(this.allDeviceData[i].id == newId) {
							this.contextualModelItem.deviceName = this.allDeviceData[i].name;
						}
					}
					this.contextualModelList.push(this.contextualModelItem);
				}
			}
		},
		created() {
			var data = {
				isPage: false
			};
			this.getBuildDataInTree();
			//获取全部的设备来选择
			this.$httpRequest.get("api/Device/GetList", data, response => {
					if(response.isSuccess) {
						this.allDeviceData = response.data;
					} else {}
				}),
				this.getData();
		}
	};
</script>

<style>
	@import url("//unpkg.com/element-ui@2.4.7/lib/theme-chalk/index.css");
</style>